
/* -----------------
* brief 
* 1. this is code gen by tools 
* 2. array like [a,b,c] after each field, define the ui html tag in view, 
		a for title, 
		b for show or not , (onlyread is trial feature)
		c for element html tag , currently support : check、radio、select、input、textarea、upload
* 3. XXX
*/
//go:generate legogen -type=MchUserMemberCard -kv=ModuleName=merchantCard,PrjName=saas_sys_base -dirconf=../../.. ./MchUserMemberCard.go

/*使用方法一(go generate)：在工程root目录下，执行go generate ./modules/merchantCard/models

使用方法二(legogen)：直接调用生成命令
legogen -type=MchUserMemberCard -kv=ModuleName=merchantCard,PrjName=saas_sys_base -dirconf=./. ./modules/merchantCard/models/MchUserMemberCard.go
*/

package merchantCard
import (
	wxcardlib "github.com/chanxuehong/wechat/mp/card"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
	"time"
)

func init() {
	lego.AddBeanToSynList(new(MchUserMemberCard))
}

/* table: "tb_mch_user_member_card"
 * -----------------
*/
type MchUserMemberCard struct {
	Id        int64  `xorm:"pk" json:",string"`                  // ["id","hide","hidden"]
	IfDel     int `xorm:"default 0"` // ["deleted","hide","hidden"]
	Cdate     string `xorm:"DateTime created"`    // ["create","show","datetime"]
	Udate     string `xorm:"DateTime updated"`    // ["update","show","datetime"]
    // add your custom field here
	//Version int `xorm:"version"`  //乐观锁
	common.Who `xorm:"extends"`

	MchCardId int64 `xorm:"NOT NULL"` //["商户卡ID","show","input"]

	WeappId string `xorm:"varchar(32) NOT NULL unique(grp_uqe_card)"` // ["微信APP ID","show","input"]
	UserCardCode        string `xorm:"varchar(32) unique(grp_uqe_card)"` // ["微信code序列号","show","input"]code 序列号. 自定义code 及非自定义code的卡券被领取后都支持事件推送.
	WxCardId              string `xorm:"varchar(32) unique(grp_uqe_card)"` //  ["微信卡ID","show","input"]

	Status  wxcardlib.USER_CARD_STATUS `xorm:"varchar(32) NOT NULL"`                      // ["状态","show","input"]

	IsGiveByFriend      int    `xorm:""`                                 // ["是否转赠","show","input"]是否为转赠, 1 代表是, 0 代表否.
	FriendUserName      string `xorm:"varchar(32)"`                      // ["赠送方账号OpenID","show","input"] 赠送方账号(一个OpenID), "IsGiveByFriend"为1 时填写该参数.

	OldUserCardCode     string `xorm:"varchar(64)"`                      // ["转赠前的code序列号","show","input"]转赠前的code序列号。
	OuterId             int64  `xorm:"" json:",string"`                  // ["领取场景值","show","input"] 领取场景值, 用于领取渠道数据统计. 可在生成二维码接口及添加JS API 接口中自定义该字段的整型值.
	OuterStr            string `xorm:"varchar(64)"`                      // ["领取场景值str","show","input"]
	IsRestoreMemberCard int    `xorm:""`                                 // ["IsRestoreMemberCard","show","input"]
	IsRecommendByFriend int    `xorm:""`                                 // ["IsRecommendByFriend","show","input"]
	UnionId             string `xorm:"varchar(32)"`                      // ["UnionId","show","input"]

	//核销相关字段
	ConsumeData `xorm:"extends"`

	MemberInfo `xorm:"extends" json:"MemberInfo"`

	MemberLevelNo int  // ["会员级别","show","input"]
	Bonus int `xorm:""` // ["积分","onlyread","input"]
	Balance int `xorm:""` // ["余额","onlyread","input"]


}

type MchUserMemberCard_Feild struct {
    Bonus string
	Balance string

	//MemberInfo结构内的字段
    UserName string
    Gender  string
    Birthday string
    Phone string

	MemberLabel string
    WechatId string
    Comment string
}

var Feild_MchUserMemberCard = &MchUserMemberCard_Feild{
	Bonus: "bonus",
	Balance: "balance",

	//MemberInfo结构内的字段
	UserName: "user_name",
	Gender: "gender",
	Birthday:"birthday",
	Phone:"phone",
	MemberLabel:"member_label",
	WechatId:"wechat_id",
	Comment:"comment",
}

type GenderType int
const (
	GenderType_Male GenderType = 0
	GenderType_Female GenderType = 1
)

//会员信息
type MemberInfo struct {
	UserName string `xorm:"" json:"user_name"`
	Gender  GenderType `xorm:"" json:"gender"`
	Birthday time.Time `xorm:"" json:"birthday"`
	Phone string `xorm:"" json:"phone"`

	MemberLabel []string `xorm:"" json:"member_label"`//会员标签
	WechatId string `xorm:"" json:"wechat_id"`//微信号
	Comment string `xorm:"" json:"comment"`//备注


}

type ConsumeData struct {
	//核销相关字段
	ConsumeSource string `xorm:""` // ["ConsumeSource","show","input"] 核销来源。支持开发者统计API核销（FROM_API）、公众平台核销（FROM_MP）、卡券商户助手核销（FROM_MOBILE_HELPER）（核销员微信号）
	LocationName  string `xorm:""` // ["LocationName","show","input"] 门店名称，当前卡券核销的门店名称（只有通过自助核销和买单核销时才会出现该字段）
	StaffOpenId   string `xorm:""` // ["StaffOpenId","show","input"] 核销该卡券核销员的openid（只有通过卡券商户助手核销时才会出现）
	VerifyCode    string `xorm:""` // ["VerifyCode","show","input"] 自助核销时，用户输入的验证码
	RemarkAmount  string `xorm:""` // ["RemarkAmount","show","input"]
}


//这个New函数用于微信推送的场景
func NewMchUserMemberCard(who *common.Who, weappid string, memberCard *MchMemberCard, getCardEvt *wxcardlib.UserGetCardEvent, memberInfo *MemberInfo) *MchUserMemberCard {
	mchUserMemberCard := new(MchUserMemberCard)
	if getCardEvt != nil { //来自微信侧推送的消息
	    mchUserMemberCard.WxCardId = getCardEvt.CardId
	    mchUserMemberCard.IsGiveByFriend = getCardEvt.IsGiveByFriend
	    mchUserMemberCard.FriendUserName = getCardEvt.FriendUserName
	    mchUserMemberCard.UserCardCode = getCardEvt.UserCardCode
	    mchUserMemberCard.OldUserCardCode = getCardEvt.OldUserCardCode
	    mchUserMemberCard.OuterId = getCardEvt.OuterId
	    mchUserMemberCard.OuterStr = getCardEvt.OuterStr
	    mchUserMemberCard.IsRestoreMemberCard = getCardEvt.IsRestoreMemberCard
	    mchUserMemberCard.IsRecommendByFriend = getCardEvt.IsGiveByFriend
	    mchUserMemberCard.UnionId = getCardEvt.UnionId
	} else {
		//纯商户卡才会进入这个分支
		mchUserMemberCard.MemberInfo = *memberInfo
	}

	mchUserMemberCard.Status = wxcardlib.UserCardStatusValid
	mchUserMemberCard.Who = *who
	mchUserMemberCard.MchCardId = memberCard.Id
	mchUserMemberCard.Bonus = memberCard.MemberCard.BonusRule.InitIncreaseBonus //初始积分
	mchUserMemberCard.Balance = 0 //初始余额

	return mchUserMemberCard
}


// deprecated 废除
func NewMchUserMemberCardFromMchMemberCard(who *common.Who, memberCard *MchMemberCard) *MchUserMemberCard {
	mchUserMemberCard := new(MchUserMemberCard)
	mchUserMemberCard.Who = *who
	mchUserMemberCard.MchCardId = memberCard.Id
	mchUserMemberCard.Bonus = memberCard.MemberCard.BonusRule.InitIncreaseBonus //初始积分
	mchUserMemberCard.Balance = 0 //初始余额
	return mchUserMemberCard
}

func (t *MchUserMemberCard) SetConsume(consumeEvt *wxcardlib.UserConsumeCardEvent) error {
	t.ConsumeData.ConsumeSource = consumeEvt.ConsumeSource
	t.ConsumeData.LocationName = consumeEvt.LocationName
	t.ConsumeData.StaffOpenId = consumeEvt.StaffOpenId
	t.ConsumeData.VerifyCode = consumeEvt.VerifyCode
	t.ConsumeData.RemarkAmount = consumeEvt.RemarkAmount
	t.Status = wxcardlib.UserCardStatusConsumed
	return nil
}